<template>
  <div class="components-container">
    <div class="header-form" style="margin-bottom:0;">
        <div class="layui-inline">
          <label class="layui-form-label" >车牌号</label>
            <div class="layui-input-inline" style="width:185px;">
                <el-cascader :options="carnum" v-model="carLicLikes" filterable :show-all-levels="false" @change="btnSearch">
                </el-cascader>
              </div>
        </div>
      
         <div class="layui-inline">
          <label class="layui-form-label" >驾驶员</label>         
          <div class="layui-input-inline" style="width:185px;">
            <el-select v-model="selectData.cdhDricerLike" @change="btnSearch">
              <el-option label="不选择" :value="null"></el-option>
              <el-option v-for="item in drivers" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </div>
        </div>
        <div class="layui-inline">
          <label class="layui-form-label" >派车地点</label>
          <div class="layui-input-inline">
              <el-input v-model="selectData.cdhadressLike" placeholder="派车地点" @keydown.enter.native="btnSearch"></el-input>
          </div>
        </div>
    </div>
    <div class="header-form" style="margin-top:10px;">
        <div class="layui-inline">
            <label class="layui-form-label" >出车日期</label>
            <div class="layui-input-inline"> 
              <el-date-picker v-date v-model="selectData.CdhactualdateGreaterAndEquals" type="date" placeholder="请选择日期大于该日期" @change="btnSearch">
              </el-date-picker>
            </div>
            <div class="layui-input-inline">
              <el-date-picker v-date v-model="selectData.CdhactualdateLessAndEquals" type="date" placeholder="请选择日期小于该日期" @change="btnSearch">
              </el-date-picker>
            </div>
        </div>
      <el-button type="primary" @click="btnSearch" :loading="this.$store.state.isloading">搜索</el-button>
      <el-button  @click="btnRepeat">重置</el-button>
    </div>
    <!-- 功能列表 -->
    <div style="margin-top:10px;">
      <ul class="toolList">
        <li><el-button type="primary" @click="btnSearch" :loading="this.$store.state.isloading">刷新</el-button></li>
        <li v-if="btnShow(item.BTNVALUE)" v-for="(item, index) in $store.state.btnListCommon" @click="operate(item.BTNVALUE)">
            <el-button type="primary" :value="item.BTNSHOWNAME" v-text="item.BTNSHOWNAME"></el-button>
        </li>
      </ul>
    </div>
    <!-- 数据列表 -->
    <dataTable :height="tableHeight" :isIndex="true" :hiddenPageOrder="false" :headList="headList" :dataList="dataList" :pageOrder="pageOrder" @pageChange="pageChange" @selectRow="selectRow"></dataTable>
    <carSendMain ref="dialog" :orderType="order" @refreshData="getData"></carSendMain>

  </div>
</template>

<script>
import carSendMain from "./carSendMainyy";
import apiBase from "@/api/apiBase.js";
import dataTable from "@/components/Table/DataTable";
import { formatDate, timeStringToDate } from "@/utils/date.js";
import { manage } from "@/mixins/managePage.js";
const ADDS = "派车";
const MODIFY = "修改";
export default {
  name: "carSendHistory",
  mixins: [manage],
  data() {
    return {
      tableHeight: "600",
      //查询条件
      selectData: {
        CdhactualdateGreaterAndEquals: null,
        CdhactualdateLessAndEquals: null,
        CdhcodnameEquals: null
      },
      sendCarTime: null,
      cdhdata: {},
      drivers: [],
      carnum: [],
      carLicLikes: [],
      title: null,
      message: null,
      pdfUrl: null,
      pageWidth: 1250,
      maxHeight: 550,
      isShow: null,
      Kilometer: 0,
      OutDataExcel: [],
    };
  },
  methods: {
    //功能操作
    operate(order) {
      this.order = order;
      switch (this.order) {
        //余姚需求
        case ADDS:
          this.$refs.dialog.open(this.order);
          break;
        case MODIFY:
          this.$refs.dialog.open(this.order, this.currentRow.CDHID);
          break;
        case "详情":
          this.$refs.dialog.open(this.order, this.currentRow.CDHID);
          break;
        case "删除":
          this.$refs.dialog.open(this.order, this.currentRow.CDHID);
          break;
        default:
      }
    },
    //导出excel
    exportExcel() {
      var startTime;
      var endTime;
      if (this.selectData.CdhactualdateGreaterAndEquals) {
        startTime = this.selectData.CdhactualdateGreaterAndEquals.toLocaleDateString();
      } else {
        startTime = "空";
      }
      if (this.selectData.CdhactualdateLessAndEquals) {
        endTime = this.selectData.CdhactualdateLessAndEquals.toLocaleDateString();
      } else {
        endTime = "空";
      }
      var header = "出车汇总" + "(" + startTime + "--" + endTime + ")";
      if (this.dataList.length === 0) {
        this.$message.error("无导出数据！");
        return;
      }
      this.downloadLoading = true;
      // this.OutDataExcel.push({CDHSTATUSDESC:"合计",CDHMILEAGE:this.Kilometer})
      require.ensure([], () => {
        const { export_json_to_excel } = require("@/vendor/Export2Excel");
        export_json_to_excel(this.headList, this.OutDataExcel, header);
        this.downloadLoading = false;
      });
    },
    //打印出车汇总
    copycarhz() {
      let Data = JSON.parse(JSON.stringify(this.selectData));
      Data.CdhactualdateGreaterAndEquals = formatDate(
        Data.CdhactualdateGreaterAndEquals,
        "yyyyMMdd"
      );
      Data.CdhactualdateLessAndEquals = formatDate(
        Data.CdhactualdateLessAndEquals,
        "yyyyMMdd"
      );
      if (this.carLicLikes.length != 0) {
        Data.carLicLike = this.carLicLikes[1];
      } else {
        Data.carLicLikes = null;
      }
      let data = {
        Data: Data
      };
      apiBase.DoService(
        data,
        apiBase.getApiAdress().Pdf.PrintCollectionofcars,
        res => {
          this.pdfUrl = res.data.content.PDFNAME;
          this.$refs.dialog1.open();
        }
      );
    },
    //获取表头
    getSendCarColumns() {
      let data = {};
      apiBase.DoService(
        data,
        apiBase.getApiAdress().Column.GetSendCarOtherColumns,
        res => {
          this.headList = res.data.content;
          // if(this.isShow === true){
          //   this.headList.splice(9,1)
          // }
        }
      );
    },
        btnShow(btn){
        switch (btn) {
        case '派车':
        return true;
        break;
        case '详情':
        if(this.currentRow) return true;
        break;
        case '修改': //余姚 
        if(this.currentRow) return true;
        break;
        case '删除': //余姚 
        if(this.currentRow) return true;
        default: return false;
          }
      },
    // 获取车辆信息
    getcarnum() {
      let data = {};
      apiBase.DoService(
        data,
        apiBase.getApiAdress().Car.QueryNormalCarByType,
        res => {
          let cardata = res.data.content;
          let legendsCarData = cardata.map(item => ({
            label: item.CodeName,
            value: item.CodeValue,
            children: item.list_Cars.map(caritem => ({
              label: caritem.CARLIC,
              value: caritem.CARLIC
            }))
          }));
          this.carnum = legendsCarData;
        }
      );
    },

    //获取派车信息
    getData() {
      let Data = JSON.parse(JSON.stringify(this.selectData));
      Data.CdhactualdateGreaterAndEquals = formatDate(
        Data.CdhactualdateGreaterAndEquals,
        "yyyyMMdd"
      );
      Data.CdhactualdateLessAndEquals = formatDate(
        Data.CdhactualdateLessAndEquals,
        "yyyyMMdd"
      );
      if (this.carLicLikes.length != 0) {
        Data.carLicLike = this.carLicLikes[1];
      } else {
        Data.carLicLikes = null;
      }
      let data = {
        PageIndex: -1,
        PageSize: 0,
        //  PageSize:200,
        OrderBy: this.pageOrder.orderType,
        OrderByType: this.pageOrder.orderSort,
        Data: Data
      };
      apiBase.DoService(
        data,
        apiBase.getApiAdress().Sendcar.QuerySendCarOther,
        res => {
          this.dataList = res.data.content.OutData;
          this.pageOrder.totalNum = res.data.content.RecordCount;
          this.$nextTick(function() {
              let rows = document.getElementsByClassName("el-table__row");
              if(this.ischangered){
                this.dataList.forEach((item,index) => {
                    rows[index].style.color = '#606266';
                    if(item.REGCRETYPE === 'A'){
                        rows[index].style.color = 'red';
                    }
                });
              };
              if(this.ischangnb){
                this.dataList.forEach((item,index) => {
                    rows[index].style.color = '#606266';
                    if(item.REGCRETYPE === 'A'||item.REGCRETYPE === 'C'){
                        rows[index].style.color = '#007eff';
                    }
                    if(item.REGCRETYPE === 'D'){
                        rows[index].style.color = 'red';
                    }
                });
              };
          });
        }
      );
    },

    //派车管理 搜索框重置
    btnRepeat() {
      this.selectData = {
        CdhactualdateGreaterAndEquals: null,
        CdhactualdateLessAndEquals: null,
        CdhcodnameEquals: null
      };
      this.carLicLikes = [];
      this.selectData.CdhactualdateGreaterAndEquals = new Date();
      this.selectData.CdhactualdateLessAndEquals = new Date();
      this.getData();
    },
    //获取司机
    getDrivers() {
      let data = {};
      apiBase.DoService(data, apiBase.getApiAdress().Staff.QueryDriver, res => {
        let driverData = res.data.content,
          legendsDrivers = driverData.map(item => ({
            label: item.STANAME,
            value: item.STAID
          }));
        this.drivers = legendsDrivers;
      });
    },
    //宁波个性化 控制显隐
    getningbo() {},
    //详情 --派车
    dataDetail() {
      var data = {
        CDHID: this.currentRow.CDHID
      };
      apiBase.DoService(data, apiBase.getApiAdress().Sendcar.Detail, res => {
        this.cdhdata = res.data.content;
        this.sendCarTime = timeStringToDate(
          this.cdhdata.CDHACTUALDATE,
          this.cdhdata.CDHACTUALTIME
        );
      });
    }
  },
  components: {
    carSendMain,
    dataTable,
  },
  created() {
    this.selectData.CdhactualdateGreaterAndEquals = new Date();
    this.selectData.CdhactualdateLessAndEquals = new Date();
    // this.selectData.CdhactualdateGreaterAndEquals.setDate(1);
    apiBase.DoService(
      {},
      apiBase.getApiAdress().Param.GetIsShowOutOfTime,
      res => {
        this.isShow = res.data.content;
        //获取表头
        this.getSendCarColumns();
      }
    );
    //派车管理 列表
    this.getData();
    this.getDrivers();
    this.getcarnum();
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
@import "src/styles/common.scss";
</style>